home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11090 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: apoll.informatik.uni-bonn.de!Mars!schregle
  2. From: schregle@Mars (Roland Schregle)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Virtual Base Class
  5. Date: 12 Mar 1996 16:46:20 GMT
  6. Organization: Rheinische Friedrich-Wilhelms-Universit"at Bonn, Institut
  7. Message-ID: <4i49os$20j@apoll.informatik.uni-bonn.de>
  8. References: <313F98D0.102E@ucla.edu> <4i1k92$3n8@apoll.informatik.uni-bonn.de>
  9. NNTP-Posting-Host: mars.informatik.uni-bonn.de
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Yep, I'm replying to myself... 
  13.  
  14. Roland Schregle (schregle@zeus.informatik.uni-bonn.de) wrote:
  15. : Dennis Rahaman (dennisr@ucla.edu) wrote:
  16. : : This is what I want to do:
  17.  
  18. : :               a
  19. : :             /   \
  20. : :            b     c
  21. : :             \   /
  22. : :               d
  23.  
  24.  
  25. : : class a
  26. : :   {
  27. : :   //...
  28. : :   };
  29.  
  30. : : class b : public virtual a
  31. : :   {
  32. : :   //...
  33. : :   };
  34.  
  35. : : class c : public virtual b
  36. Judging by the diagram, I would assume this class is derived from
  37. class a, NOT b.
  38. : :   {
  39. : :   //...
  40. : :   };
  41.  
  42. : : class d : public b, public c
  43. : :   {
  44. : :   //...
  45. : :   };
  46.  
  47.  
  48. : : void f ()
  49. : :   {
  50. : :   a a1;
  51. : :   d* pd = (d*) &a1;  // error: can't cast virtual base to derived
  52. : :   }
  53.  
  54. : : ///////////////////////////////////////////////////////////
  55. : : Can someone explain why I can't cast a virtual base class to a derived 
  56. : : class?
  57.  
  58. : : What should I do instead?
  59.  
  60. : : Any help would be appreciated.
  61.  
  62.  
  63.  
  64. : I have the same problem! Somebody suggested the following (in terms of the
  65. : above egg sample):
  66.  
  67. : void f()
  68. : {
  69. :    a a1;
  70. :    d* pd = (d*)(void*)&a1;
  71. : }
  72.  
  73. : I haven't tried this yet. C'mon you C++ cracks out there!!! :)
  74.  
  75.  
  76. : GanjaTron
  77.  
  78. Actually, GanjaTron, I *did* try it out, however it requires adding an
  79. offset to the void*:
  80.  
  81. void f()
  82. {
  83.    a a1;
  84.    d* testy = new d; // :)
  85.    int offset = (void*)testy - (void*)(a*)testy;
  86.    delete testy;
  87.    d* pd = (d*)((void*)a1 + offset);
  88. }
  89.  
  90. The offset specifies the difference of the addresses where the instance
  91. of the derived class and that of the virtual base class within it are
  92. stored. (I didn't come up with this method, so correct me if I'm wrong
  93. or totally vague).
  94.  
  95. This ALLEGEDLY works on most (all?) compilers, I only tried it with GNU.
  96. Needless to say, it's FILTHY code!
  97.  
  98. So THERE, GanjaTron! :)
  99.  
  100. GanjaTron
  101.